/*
* CleanController.java
* Created on  202015/6/15 16:35
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/15       longshihui    初始版本
*
*/

package com.infitecs.eyas.combustion;

import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/combustion/clean")
public class CleanController {
    @Autowired
    private CleanService cleanService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;
    @Autowired
    private BurnService burnService;

    @RequestMapping("")
    public String getCleanPage( HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "combustion/clean";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6 ) {
            request.setAttribute("step", step);
            return "combustion/clean";
        }
        step = stepService.geRsStep(caseId);
        if (step < 11) {
            request.setAttribute("step", step);
            return "combustion/clean";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        CalculateClean calculateClean = cleanService.getCalculateClean(caseId);
        if (calculateClean == null) {
            Map<String, Double> dataMap = burnService.getBurnDataByCaseId(caseId);
            request.setAttribute("dataMap", dataMap);
        }else {
            Map<String, Double> dataMap = cleanService.getCleanDataByCaseId(caseId);
            request.setAttribute("dataMap", dataMap);
        }
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("calculateClean", calculateClean);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "combustion/clean";
    }

    @RequestMapping(value = "/saveValueOfCleanEx", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfCleanEx(CalculateClean calculateClean) {
        cleanService.saveValueOfClean(calculateClean);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putCleanValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "/cal254", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse cal254() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Double e253 = expressionService.calculate(new Param("RE253"));
        Double e254 = expressionService.calculate(new Param("RE254"));
        Double e255 = expressionService.calculate(new Param("RE255"));
        ajaxResponse.addAttribute("E253",e253);
        ajaxResponse.addAttribute("E254",e254);
        ajaxResponse.addAttribute("E255",e255);
        CalculateClean calculateClean = new CalculateClean();
        calculateClean.setParamName("E253");
        calculateClean.setParamValue(e253);
        cleanService.saveValueOfClean(calculateClean);
        calculateClean.setParamName("E254");
        calculateClean.setParamValue(e254);
        cleanService.saveValueOfClean(calculateClean);
        calculateClean.setParamName("E255");
        calculateClean.setParamValue(e255);
        cleanService.saveValueOfClean(calculateClean);
        return ajaxResponse;
    }

    @RequestMapping(value = "/cal244", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse cal244() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Double e253 = expressionService.calculate(new Param("RE244"));
        Double e254 = expressionService.calculate(new Param("RE331"));
        ajaxResponse.addAttribute("E244",e253);
        ajaxResponse.addAttribute("E331",e254);
        CalculateClean calculateClean = new CalculateClean();
        calculateClean.setParamName("E244");
        calculateClean.setParamValue(e253);
        cleanService.saveValueOfClean(calculateClean);
        calculateClean.setParamName("E331");
        calculateClean.setParamValue(e254);
        cleanService.saveValueOfClean(calculateClean);
        return ajaxResponse;
    }

    @RequestMapping(value = "cleanSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse cleanSave(CalculateClean calculateClean) {
        try {
            cleanService.cleanSave(calculateClean);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
